﻿using BAL;
using DAL;
using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Module.QLHoaDon
{
    public partial class frmChiTietHoaDon : Form
    {
        private HoaDon passDataHD;

        public frmChiTietHoaDon()
        {
            InitializeComponent();
        }

        public frmChiTietHoaDon(HoaDon hoaDon)
        {
            InitializeComponent();
            passDataHD = hoaDon;
            XemChiTietHoaDon(passDataHD);
        }

        private void frmChiTietHoaDon_Load(object sender, EventArgs e)
        {
            //Console.WriteLine("Load");
            XemChiTietHoaDon(passDataHD);
            btnUpdateChiTietHD.Enabled = false;
            btnDeleteChiTietHD.Enabled = false;
            if (Convert.ToInt32(txtTienDua.Text) < Convert.ToInt32(txtTongTien.Text))
            {
                btnExitChiTiet.Enabled = false;
                btnUpdatePrice.Enabled = false;
            }
        }

        public void XemChiTietHoaDon(HoaDon hoadon)
        {
            //SqlCommand cmd = new SqlCommand("select HangHoa.MaHang,HangHoa.TenHang, HangHoa.SoLuongDongGoi, HangHoa.DonVi, HangHoa.VAT,ChiTietHoaDon.GiaTruocVAT" +
            //                                            ", ChiTietHoaDon.GiatruocVat*HangHoa.Vat/100+ChiTietHoaDon.GiaTruocVAT as ThanhTien from " +
            //                                            "HangHoa, ChiTietHoaDon where MaHoaDon=@maHD and HangHoa.MaHang=ChiTietHoaDon.MaHang", DataAccess.GetConnection());
            SqlCommand cmd = new SqlCommand("select MaHang,TenHang,SoLuongMua,DonVi, MaGiamGiaHang as MaGiamGia,GiaTruocVAT,VAT,ThanhTien " +
                "from view_ChiTietHoaDon where MaHoaDon=@MaHD", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@maHD", hoadon.maHoaDon);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgvChiTietHoaDon.DataSource = dt;

            txtMaKH.Text = passDataHD.maKhachHang;
            txtMaThuNgan.Text = passDataHD.maNVThanhToan;
            txtMaHD.Text = passDataHD.maHoaDon;
            txtTienDua.Text = passDataHD.tienKhachDua.ToString();
            dtpkNgayIn.Value = passDataHD.ngayInHoaDon;
            dtpkNgayMua.Value = passDataHD.ngayMuaHang;
            txtTenKH.Text = this.GetTenKH(passDataHD.maKhachHang);
            txtTenThuNgan.Text = this.GetTenNV(passDataHD.maNVThanhToan);

            double thanhTien = 0;
            if (dt.Rows.Count >= 1)
                for (int i = 0; i < dt.Rows.Count; i++)
                    thanhTien += Convert.ToDouble(dt.Rows[i]["ThanhTien"].ToString());
            txtTongTien.Text = thanhTien.ToString();

        }

        private string GetTenKH(string maKH)
        {
            string s = "";
            SqlCommand cmd = new SqlCommand("select TenKH from KhachHang where MaKH=@MaKH", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaKH", maKH);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            s = dt.Rows[0]["TenKH"].ToString();
            return s;
        }

        private string GetTenNV(string maNV)
        {
            string s = "";
            //Console.WriteLine("tennv:" + maNV);
            SqlCommand cmd = new SqlCommand("select * from NhanVien where MaNV=@MaNV", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaNV", maNV);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            s = dt.Rows[0]["TenNV"].ToString();
            return s;
        }

        private void btnDeleteChiTietHD_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult=MessageBox.Show("Ban co chac chan muon xoa", "OOPS", MessageBoxButtons.YesNo);
            if (DialogResult.Yes==dialogResult)
            {
                SqlCommand cmd = new SqlCommand("delete from ChiTietHoaDon where MaHang=@Mahang and MaHoaDon=@maHD", DataAccess.GetConnection());
                cmd.Parameters.AddWithValue("@Mahang", dgvChiTietHoaDon.SelectedRows[0].Cells[0].Value.ToString());
                cmd.Parameters.AddWithValue("@MaHD", txtMaHD.Text);
                int flag = cmd.ExecuteNonQuery();
                if (flag == 1)
                    MessageBox.Show("Xoa thanh cong !");
                else
                    MessageBox.Show("Xoa ko thanh cong !");
            }
        }

        private void btnAddChiTietHD_Click(object sender, EventArgs e)
        {
            frmThemChiTietHoaDon frm = new frmThemChiTietHoaDon(txtMaHD.Text);
            frm.ShowDialog();
        }
        
        ChiTietHoaDonBAO ctBAO = new ChiTietHoaDonBAO();

        private void btnUpdateChiTietHD_Click(object sender, EventArgs e)
        {
            ChiTietHoaDon obj = new ChiTietHoaDon();
            obj.maHoaDon = txtMaHD.Text;
            obj.maHang = dgvChiTietHoaDon.SelectedRows[0].Cells[0].Value.ToString();
            obj.soLuongMua = Convert.ToInt32(dgvChiTietHoaDon.SelectedRows[0].Cells[2].Value.ToString());
            obj.maGiamGiaHang = dgvChiTietHoaDon.SelectedRows[0].Cells[4].Value.ToString();
            obj.giaTruocVAT = Convert.ToInt32(dgvChiTietHoaDon.SelectedRows[0].Cells[5].Value.ToString());
            obj.VAT = Convert.ToInt32(dgvChiTietHoaDon.SelectedRows[0].Cells[6].Value.ToString());

            frmThemChiTietHoaDon frm=new frmThemChiTietHoaDon(obj);
            frm.ShowDialog();
        }

        private void btnExitChiTiet_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvChiTietHoaDon_Click(object sender, EventArgs e)
        {

            btnUpdateChiTietHD.Enabled = true;
            btnDeleteChiTietHD.Enabled = true;
        }

        private void txtTienDua_TextChanged(object sender, EventArgs e)
        {
            Console.WriteLine("TD" + txtTienDua.Parent.ToString());
            try
            {
                if (txtTienDua.Text != "")
                    if (Convert.ToInt32(txtTienDua.Text) >= Convert.ToInt32(txtTongTien.Text))
                    {
                        btnExitChiTiet.Enabled = true;
                        btnUpdatePrice.Enabled = true;
                    }
                    else
                    {
                        btnExitChiTiet.Enabled = false;
                        btnUpdatePrice.Enabled = false;
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Du lieu nhap vao ko hop le!");
                MessageBox.Show(ex.Message);
                txtTienDua.Text = txtTongTien.Text;
                txtTienDua.Focus();
            }
        }

        private void txtTienDua_Click(object sender, EventArgs e)
        {
            txtTienDua.TextChanged += txtTienDua_TextChanged;
        }

        HoaDonBAO hdBAO = new HoaDonBAO();
        private void btnUpdatePrice_Click(object sender, EventArgs e)
        {
            passDataHD.tienKhachDua = Convert.ToInt32(txtTienDua.Text);
            hdBAO.UpdateHoaDon(passDataHD);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            dgvChiTietHoaDon.Refresh();
            XemChiTietHoaDon(passDataHD);
            if (Convert.ToInt32(txtTienDua.Text) < Convert.ToInt32(txtTongTien.Text))
            {
                btnExitChiTiet.Enabled = false;
                btnUpdatePrice.Enabled = false;
            }
        }

    }
}
